package com.ywgt.forkjoin;

import java.util.concurrent.RecursiveTask;

public class ForkJoinTest extends RecursiveTask<Long> {

    private Long start;

    private Long end;

    private static final Long i=10000L;

    public ForkJoinTest(Long start, Long end) {
        this.start = start;
        this.end = end;
    }

    @Override
    protected Long compute() {
        if (end-start<i){
            Long sum=0L;
            for (Long i = 1L; i <end; i++) {
                sum+=i;
            }
            return sum;
        }else {
         Long middle=(start+end)/2;
         ForkJoinTest forkJoinTest1=new ForkJoinTest(start,middle);
         forkJoinTest1.fork();
         ForkJoinTest forkJoinTest2=new ForkJoinTest(middle+1,end);
         forkJoinTest2.fork();
         return forkJoinTest1.join()+forkJoinTest2.join();
        }
    }
}
